home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / CDTV / cdtvtools-11 / system / readfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-24  |  4.8 KB  |  147 lines

  1. /***********************************************************************
  2. ***
  3. *** CDTV - Read a File Directly from CD-ROM
  4. ***
  5. ***     Copyright (C) 1990 Commodore-Amiga, Inc.
  6. ***     Permission granted for use in CDTV applications.
  7. ***     Author: Carl Sassenrath, Ukiah, CA  (30-DEC-90)
  8. ***
  9. ************************************************************************
  10. ***
  11. ***     What:
  12. ***       Sometimes you may want to read a file directly from
  13. ***       disk without passing through the file system. This
  14. ***       example illustrates a simple technique for doing so.
  15. ***
  16. ***     Notes:
  17. ***       1. This example deals with CDFS file locks.  These
  18. ***          structures are not entirely public, and you should
  19. ***          not attempt to copy them elsewhere nor access any
  20. ***          non-public fields within them.
  21. ***       2. This example only works if you know the sector size
  22. ***          of the disk you are reading.  Normally they are 2048
  23. ***          bytes in length, but other sizes are possible. You
  24. ***          should limit your use of this code to your disks only.
  25. ***       3. You should think about what happens when the disk is
  26. ***          ejected... see the note below.
  27. ***       4. Currently tested with Manx C compiler only.
  28. ***
  29. ***
  30. ***********************************************************************/
  31.  
  32. #include <exec/types.h>
  33. #include <exec/io.h>
  34. #include <devices/cdtv.h>
  35. #include <libraries/dos.h>
  36. #include <libraries/dosextens.h>
  37.  
  38. extern  struct  IOStdReq *CreateStdIO();
  39. extern  struct  MsgPort  *CreatePort();
  40. struct  IOStdReq *IOReq1 = NULL;
  41. struct  MsgPort  *IOPort = NULL;
  42.  
  43. char    Buffer[10*2048];
  44.  
  45. char    AssertFail[] = "Assertion failed (MUST)";
  46. #define MUST(expr)  if (!(expr)) Quit(AssertFail);
  47.  
  48.  
  49. /***********************************************************************
  50. ***
  51. ***  Main
  52. ***
  53. ***********************************************************************/
  54. main(argc,argv)
  55.         int argc;
  56.         char *argv[];
  57. {
  58.         ULONG l;
  59.         ULONG sector;
  60.         struct FileLock *lock;
  61.  
  62.         printf("CDTV Direct File Read Example (30-Dec-90)\n");
  63.  
  64.         Init();
  65.  
  66.         /* Convert path/file name into something the FS understands: */
  67.         l = Lock("s:startup-sequence",ACCESS_READ);
  68.         if (!l) Quit("Cannot lock the file");
  69.         lock = (struct FileLock *)BADDR(l); /* Convert to address */
  70.  
  71.         /* Pluck the sector right out of public part of the structure */
  72.         sector = lock->fl_Key;
  73.         UnLock(l); /* Lock freed - "l" & "lock" not valid anymore. */
  74.  
  75.         /* Now simply read the sectors needed... HOWEVER, you may want
  76.         ** to keep an eye out for a disk change since the file system
  77.         ** can no longer do so for you!  The longer you wait after the
  78.         ** lock has been made, the better the chance that the disk has
  79.         ** been changed, and your read may be successful, but on the
  80.         ** wrong disk.  Use the AmigaDOS packet ACTION_INFO to check
  81.         ** what disk is present.
  82.         */
  83.         DoIOR(IOReq1, CD_READ, sector*2048, 2048, Buffer);
  84.         puts(Buffer);
  85.         /*
  86.         ** This READ must begin at an even offset (word aligned)
  87.         ** and be an even number of bytes in length. Also the buffer
  88.         ** must be word aligned. Another example might be:
  89.         */
  90.         DoIOR(IOReq1, CD_READ, sector*2048+5632, 12320, Buffer);
  91.         /* For best performance, make the read as long as possible. */
  92.  
  93.         Quit(0);
  94. }
  95.  
  96. /***********************************************************************
  97. ***
  98. ***  Init -- initialize program and structures
  99. ***
  100. ***********************************************************************/
  101. Init()
  102. {
  103.         MUST(IOPort = CreatePort(0,0));
  104.         MUST(IOReq1 = CreateStdIO(IOPort));
  105.  
  106.         if (OpenDevice("cdtv.device",0,IOReq1,0))
  107.                 Quit("CDTV Device will not open");
  108. }
  109.  
  110. /***********************************************************************
  111. ***
  112. ***  Quit -- exit program and clean-up.  Return an error if needed.
  113. ***
  114. ***********************************************************************/
  115. Quit(s)
  116.         char *s;        /* error message */
  117. {
  118.         if (IOReq1)
  119.         {
  120.                 if (IOReq1->io_Device) CloseDevice(IOReq1);
  121.                 DeleteStdIO(IOReq1);
  122.         }
  123.         if (IOPort)     DeletePort(IOPort);
  124.         if (s) {printf("\nERROR: %s\n",s); exit(40);}
  125.         else exit(0);
  126. }
  127.  
  128. /***********************************************************************
  129. ***
  130. ***  DoIOR -- execute a device command
  131. ***
  132. ***********************************************************************/
  133. DoIOR(req,cmd,off,len,data)
  134.         struct IOStdReq *req;
  135.         int cmd;
  136.         long off;
  137.         long len;
  138.         APTR data;
  139. {
  140.         req->io_Command = cmd;
  141.         req->io_Offset = off;
  142.         req->io_Length = len;
  143.         req->io_Data   = data;
  144.         if (DoIO(req)) Quit("DoIO command error");
  145. }
  146.  
  147.